package q0189;

/**
 * 反转数组
 *
 * @author fangjiaxiaobai
 * @date 2021-08-15 15:56
 * @since 2.0.0
 */
public class Solution {

    /**
     * 反转数组
     *
     * @param nums 数组列表
     * @param k    第k个元素后反转
     */
    public static void rotate(int[] nums, int k) {
        if (nums == null || k < 1) {
            return;
        }
        if (nums.length < k) {
            k = k % nums.length;
        }
        int length = nums.length;
        for (int i = 0; i < k; i++) {
            int temp = nums[length - 1];
            System.arraycopy(nums, 0, nums, 1, length - 1);
            nums[0] = temp;
        }
    }

    public static void main(String[] args) {
        demo1();
        demo2();
        demo3();
        demo4();
    }

    private static void demo4() {
        int[] nums = new int[]{1,2};

        rotate(nums, 3);

        print(nums);
    }

    private static void demo3() {
        int[] nums = new int[]{-1,-100,3,99};

        rotate(nums, 5);

        print(nums);

    }

    private static void demo2() {
        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7};

        rotate(nums, 3);

        print(nums);
    }

    private static void demo1() {

        int[] nums = new int[]{0, 1, 2, 3, 4, 5};

        rotate(nums, 1);

        print(nums);

    }

    public static void print(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            System.out.printf("%d\t", nums[i]);
        }
        System.out.println();
    }

}
